from time import sleep, time
from lxml import etree
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By


# 封玲菲 selenium爬取股票 练习


opt = Options()
opt.add_argument("--headless")
opt.add_argument("--disable-gpu")

edge = Chrome(options=opt)
url = 'http://quote.eastmoney.com/center/gridlist.html#global_asia'
edge.get(url)
sleep(10)
page = edge.page_source
html = etree.HTML(page)


for i in range(0,2):

    table = edge.find_elements(by=By.XPATH, value="//table[@id='table_wrapper-table']/tbody/tr")
    for tr in table:
        # 提取文本
        txt = tr.text
        arr = txt.split(' ')  # 提取插入列表

        # 国家名称
        country_name = arr[1]
        # 最新价
        latest_price = arr[2]
        # 涨跌额
        rising_falling_amounts = arr[3]
        # 涨跌幅
        percentage_change = arr[4]
        # 开盘价
        opening_price = arr[5]
        # 最高价
        maximum_price = arr[6]
        # 最低价
        bottom_price = arr[7]
        # 昨收价
        closing_price = arr[8]
        # 振幅
        amplitude = arr[9]

        print(f"{country_name},"
              f"{latest_price},"
              f"{rising_falling_amounts},"
              f"{percentage_change},"
              f"{opening_price},"
              f"{maximum_price},"
              f"{bottom_price},"
              f"{closing_price},"
              f"{amplitude}")

    next = edge.find_elements(by=By.XPATH, value='// *[ @ id = "main-table_paginate"] / a[2]')[0]
    next.click()
    sleep(3)





# #国家名称
# country_name = html.xpath("//table[@id='table_wrapper-table']/tbody/tr/td[2]/a/text()")
# #最新价
# latest_price=html.xpath("//table[@id='table_wrapper-table']/tbody/tr/td[3]/span/text()")
# #涨跌额
# rising_falling_amounts=html.xpath("//table[@id='table_wrapper-table']/tbody/tr/td[4]/span/text()")
# #涨跌幅
# percentage_change=html.xpath("//table[@id='table_wrapper-table']/tbody/tr/td[5]/span/text()")
# #开盘价
# opening_price=html.xpath("//table[@id='table_wrapper-table']/tbody/tr/td[6]/span/text()")
# #最高价
# maximum_price=html.xpath("//table[@id='table_wrapper-table']/tbody/tr/td[7]/span/text()")
# #最低价
# bottom_price=html.xpath("//table[@id='table_wrapper-table']/tbody/tr/td[8]/span/text()")
# #昨收价
# closing_price=html.xpath("//table[@id='table_wrapper-table']/tbody/tr/td[9]")
# #振幅
# amplitude=html.xpath("//table[@id='table_wrapper-table']/tbody/tr/td[10]")

# print(latest_price)














